--
-- Author: ping.wang
-- Date: 2019-01-24 14:56:36
--
-- RewardFundPanel
--

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardWelfareFundPanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    self._ui.Button_buy:addClickEventListener(function()
        --首先判断钻石是否足够
        if self._data.price > UD:getCoin() then
            local goShopDialog = display.newConfirmDialog( L("demondShop_notenough"),
                function() -- 确定
                    app:sendMsg("MallController", "openView", {tabKey = Constant.FUNC_TYPE_RECHARGE})
                end)
            UIHandler:addPopDialog( goShopDialog )
        else
            local buyDialog = display.newConfirmDialog( string.format(L("fundTips1"),self._data.price),
                function() -- 确定
                    app.session:request("action.C2S_BuyGoods", {goodsId = self._data.goodID, num = 1 }, function(msg)
                        display.pushToast(L("fundTips3"))
                    end)
                end)
            UIHandler:addPopDialog( buyDialog )
        end

    end)
    self._ui.Button_lingqu:setVisible(false)
    self._ui.Button_buy:setVisible(true)

    self._ui.Panel_money:setTouchEnabled(true)
    self._ui.Panel_money:addClickEventListener(handler(self, self._showMoneyTips))
    self._moneyNum = self._ui.Panel_money:getChildByName("num") --界面显示自己的金币数量
    self._moneyIcon = self._ui.Panel_money:findChild("icon")
    local addMoneyBtn = self._ui.Panel_money:findChild("Button_plus")
    addMoneyBtn:setVisible(true)
    addMoneyBtn:addClickEventListener(function()
        --跳转到商店
        app:sendMsg("MallController", "buyGameRes", Enums.lootType.coin)
    end)

    self:initMoneyPanel()
    self:refresh()
end

function ClassRef:initMoneyPanel()
    local coinId = Enums.lootType.coin
    local coinConfig = GD:getCoinConfig(coinId)
    local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.icon)
    self._moneyIcon:loadTexture(iconPath)
    self._moneyNum:setString(tostring(UD:getDaibiValue(coinId)))

end

--显示货币的tips
function ClassRef:_showMoneyTips()
    self:showPanelTips(self._ui.Panel_money,{tipsType = Enums.TipsType.daibi, itemId = 98})
end

function ClassRef:showPanelTips(panel, param)
    local boundingBoxTemp = panel:getBoundingBox()
    local worldPosTemp = panel:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
    boundingBoxTemp.x = worldPosTemp.x
    boundingBoxTemp.y = worldPosTemp.y
    param.boundingBox = boundingBoxTemp
    app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params = param})
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local coinConfig = GD:getCoinConfig(98)
        local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.bigicon)
        local smallIcon = string.format("%s%s.png",Res.coinIconPath,coinConfig.icon)
        self._ui.Panel_money:findChild("icon"):loadTexture(smallIcon)
        self._ui.Panel_money:findChild("num"):setString(tostring(UD:getCoin())) --显示钻石
        self._ui.Button_buy:findChild("num"):setString(string.format(L("fundTips2"),data.price)) --显示价格
        self._ui.Button_buy:findChild("name"):loadTexture(iconPath)

        if data.type == 2 then
            self._ui.num_20:setVisible(true)
            self._ui.num_25:setVisible(false)
        else
            self._ui.num_20:setVisible(false)
            self._ui.num_25:setVisible(true)
        end

        self._ui.text:setString(L(data.des))

        self:setVisible( true )
    else
        self:setVisible( false )
    end
end

function ClassRef:setMoneyPanelState(state)
    self._ui.Panel_money:setVisible(state)
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
